from barrelhouse.core.devices.models import device_type, router_module, device_status, physical_server, end_user_system, end_user_system_type, virtual_server, virtual_server_pool, router, switch, virtual_ip, virtualization_engine, component, component_type, peripheral, peripheral_type
from barrelhouse.core.networks.models import ip_v4_address
from barrelhouse.core.asset_tracking.models import asset_tag
from django.contrib import admin
from django.contrib.contenttypes import generic

class component_admin_inline(generic.GenericStackedInline):

    model = component
    extra = 1

class asset_tag_admin_inline(generic.GenericTabularInline):

    model = asset_tag
    extra = 1
    max_num = 1

class ip_v4_address_admin_inline(generic.GenericTabularInline):

    model = ip_v4_address

class vip_ip_v4_address_admin_inline(generic.GenericTabularInline):

    model = ip_v4_address
    extra = 1
    max_num = 1

class router_module_admin_inline(admin.StackedInline):

    model = router_module
    filter_horizontal = ('warranty',)
    extra = 2

class component_type_admin(admin.ModelAdmin):

    list_display = ('name', 'notes_admin')

class device_type_admin(admin.ModelAdmin):

    list_display = ('name', 'notes_admin')
    save_on_top = True

class device_status_admin(admin.ModelAdmin):

    list_display = ('name', 'notes_admin')
    save_on_top = True

class physical_server_admin(admin.ModelAdmin):

    inlines = [asset_tag_admin_inline, ip_v4_address_admin_inline, component_admin_inline ]
    list_display = ('hostname','status','asset_tag','date_added')
    filter_horizontal = ('licensed_software','opensource_software','warranty',)

class end_user_system_admin(admin.ModelAdmin):

    inlines = [asset_tag_admin_inline, ip_v4_address_admin_inline, component_admin_inline ]
    list_display = ('hostname','status','asset_tag','date_added')
    filter_horizontal = ('licensed_software','opensource_software','warranty',)

class end_user_system_type_admin(admin.ModelAdmin):

    save_on_top = True
    list_display = ('name', 'notes_admin')

class virtualization_engine_admin(admin.ModelAdmin):

    save_on_top = True
    list_display = ('name','notes_admin')

class virtual_server_admin(admin.ModelAdmin):

    inlines = [ip_v4_address_admin_inline, ]
    display_list = ('hostname', 'creation_time', 'virtual_pool', 'virtualization_method', 'status')
    readonly_fields = ('virtualization_method',)
    filter_horizontal = ('licensed_software','opensource_software',)

    def virtualization_method(self, obj):
        return obj.virtual_server_pool.virtualization_engine

class virtual_server_pool_admin(admin.ModelAdmin):

    save_on_top = True
    list_display = ('name', 'virtualization_engine')

class router_admin(admin.ModelAdmin):

    list_display = ('hostname', 'vendor', 'asset_tag', 'firmware', 'serial_number',) 
    inlines = [asset_tag_admin_inline, ip_v4_address_admin_inline, router_module_admin_inline, component_admin_inline]
    filter_horizontal = ('warranty',)

class switch_admin(admin.ModelAdmin):

    list_display = ('hostname', 'vendor', 'asset_tag', 'firmware', 'serial_number',)
    inlines = [asset_tag_admin_inline, ip_v4_address_admin_inline, component_admin_inline]
    filter_horizontal = ('warranty',)

class virtual_ip_admin(admin.ModelAdmin):

    list_display = ('hostname', 'ip_readonly',)
    readonly_fileds = ('ip_readonly',)
    inlines = [vip_ip_v4_address_admin_inline, ]

    def ip_readonly(self, obj):
        return obj.ip_v4_address.ip

class peripheral_type_admin(admin.ModelAdmin):

    save_on_top = True
    list_display = ('name','notes_admin')

class peripheral_admin(admin.ModelAdmin):

    save_on_top = True
    list_display = ('name', 'type', 'asset_tag', 'serial_number','issued_user')
    inlines = (asset_tag_admin_inline,)

class component_admin(admin.ModelAdmin):

    save_on_top = True
    inlines = (asset_tag_admin_inline,)

admin.site.register(component_type, component_type_admin)
admin.site.register(peripheral_type, peripheral_type_admin)
admin.site.register(peripheral, peripheral_admin)
admin.site.register(device_type, device_type_admin)
admin.site.register(device_status, device_status_admin)
admin.site.register(physical_server, physical_server_admin)
admin.site.register(end_user_system_type, end_user_system_type_admin)
admin.site.register(end_user_system, end_user_system_admin)
admin.site.register(virtual_server, virtual_server_admin)
admin.site.register(virtual_server_pool, virtual_server_pool_admin)
admin.site.register(virtualization_engine, virtualization_engine_admin)
admin.site.register(router, router_admin)
admin.site.register(switch, switch_admin)
admin.site.register(virtual_ip, virtual_ip_admin)
admin.site.register(component, component_admin)
